package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smackx.packet.DataForm;

public class Form
{
  public static final String TYPE_CANCEL = "cancel";
  public static final String TYPE_FORM = "form";
  public static final String TYPE_RESULT = "result";
  public static final String TYPE_SUBMIT = "submit";
  private DataForm dataForm;

  public Form(String paramString)
  {
    this.dataForm = new DataForm(paramString);
  }

  public Form(DataForm paramDataForm)
  {
    this.dataForm = paramDataForm;
  }

  public static Form getFormFrom(Packet paramPacket)
  {
    Form localForm;
    PacketExtension localPacketExtension = paramPacket.getExtension("x", "jabber:x:data");
    if (localPacketExtension != null)
    {
      DataForm localDataForm = (DataForm)localPacketExtension;
      if (localDataForm.getReportedData() == null)
        localForm = new Form(localDataForm);
    }
    while (true)
    {
      return localForm;
      localForm = null;
    }
  }

  private boolean isFormType()
  {
    return "form".equals(this.dataForm.getType());
  }

  private boolean isSubmitType()
  {
    return "submit".equals(this.dataForm.getType());
  }

  private void setAnswer(FormField paramFormField, Object paramObject)
  {
    if (!(isSubmitType()))
      throw new IllegalStateException("Cannot set an answer if the form is not of type \"submit\"");
    paramFormField.resetValues();
    paramFormField.addValue(paramObject.toString());
  }

  public void addField(FormField paramFormField)
  {
    this.dataForm.addField(paramFormField);
  }

  public Form createAnswerForm()
  {
    FormField localFormField1;
    if (!(isFormType()))
      throw new IllegalStateException("Only forms of type \"form\" could be answered");
    Form localForm = new Form("submit");
    Iterator localIterator1 = getFields();
    do
    {
      do
      {
        if (!(localIterator1.hasNext()))
          return localForm;
        localFormField1 = (FormField)localIterator1.next();
      }
      while (localFormField1.getVariable() == null);
      FormField localFormField2 = new FormField(localFormField1.getVariable());
      localFormField2.setType(localFormField1.getType());
      localForm.addField(localFormField2);
    }
    while (!("hidden".equals(localFormField1.getType())));
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator2 = localFormField1.getValues();
    while (true)
    {
      while (!(localIterator2.hasNext()))
        localForm.setAnswer(localFormField1.getVariable(), localArrayList);
      localArrayList.add((String)localIterator2.next());
    }
  }

  public DataForm getDataFormToSend()
  {
    DataForm localDataForm1;
    Iterator localIterator;
    DataForm localDataForm2;
    if (isSubmitType())
    {
      localDataForm1 = new DataForm(getType());
      localIterator = getFields();
      if (!(localIterator.hasNext()))
        localDataForm2 = localDataForm1;
    }
    while (true)
    {
      while (true)
      {
        FormField localFormField;
        do
        {
          return localDataForm2;
          localFormField = (FormField)localIterator.next();
        }
        while (!(localFormField.getValues().hasNext()));
        localDataForm1.addField(localFormField);
      }
      localDataForm2 = this.dataForm;
    }
  }

  public FormField getField(String paramString)
  {
    Object localObject;
    if ((paramString == null) || (paramString.equals("")))
      throw new IllegalArgumentException("Variable must not be null or blank.");
    Iterator localIterator = getFields();
    if (!(localIterator.hasNext()))
      localObject = null;
    while (true)
    {
      FormField localFormField;
      do
      {
        return localObject;
        localFormField = (FormField)localIterator.next();
      }
      while (!(paramString.equals(localFormField.getVariable())));
      localObject = localFormField;
    }
  }

  public Iterator<FormField> getFields()
  {
    return this.dataForm.getFields();
  }

  public String getInstructions()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    Iterator localIterator = this.dataForm.getInstructions();
    while (true)
    {
      do
      {
        if (!(localIterator.hasNext()))
          return localStringBuilder.toString();
        localStringBuilder.append((String)localIterator.next());
      }
      while (!(localIterator.hasNext()));
      localStringBuilder.append("\n");
    }
  }

  public String getTitle()
  {
    return this.dataForm.getTitle();
  }

  public String getType()
  {
    return this.dataForm.getType();
  }

  public void setAnswer(String paramString, double paramDouble)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!("text-multi".equals(localFormField.getType()))) && (!("text-private".equals(localFormField.getType()))) && (!("text-single".equals(localFormField.getType()))))
      throw new IllegalArgumentException("This field is not of type double.");
    setAnswer(localFormField, Double.valueOf(paramDouble));
  }

  public void setAnswer(String paramString, float paramFloat)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!("text-multi".equals(localFormField.getType()))) && (!("text-private".equals(localFormField.getType()))) && (!("text-single".equals(localFormField.getType()))))
      throw new IllegalArgumentException("This field is not of type float.");
    setAnswer(localFormField, Float.valueOf(paramFloat));
  }

  public void setAnswer(String paramString, int paramInt)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!("text-multi".equals(localFormField.getType()))) && (!("text-private".equals(localFormField.getType()))) && (!("text-single".equals(localFormField.getType()))))
      throw new IllegalArgumentException("This field is not of type int.");
    setAnswer(localFormField, Integer.valueOf(paramInt));
  }

  public void setAnswer(String paramString, long paramLong)
  {
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!("text-multi".equals(localFormField.getType()))) && (!("text-private".equals(localFormField.getType()))) && (!("text-single".equals(localFormField.getType()))))
      throw new IllegalArgumentException("This field is not of type long.");
    setAnswer(localFormField, Long.valueOf(paramLong));
  }

  public void setAnswer(String paramString1, String paramString2)
  {
    FormField localFormField = getField(paramString1);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if ((!("text-multi".equals(localFormField.getType()))) && (!("text-private".equals(localFormField.getType()))) && (!("text-single".equals(localFormField.getType()))) && (!("jid-single".equals(localFormField.getType()))) && (!("hidden".equals(localFormField.getType()))))
      throw new IllegalArgumentException("This field is not of type String.");
    setAnswer(localFormField, paramString2);
  }

  public void setAnswer(String paramString, List<String> paramList)
  {
    if (!(isSubmitType()))
      throw new IllegalStateException("Cannot set an answer if the form is not of type \"submit\"");
    FormField localFormField = getField(paramString);
    if (localFormField != null)
    {
      if ((!("jid-multi".equals(localFormField.getType()))) && (!("list-multi".equals(localFormField.getType()))) && (!("list-single".equals(localFormField.getType()))) && (!("hidden".equals(localFormField.getType()))))
        throw new IllegalArgumentException("This field only accept list of values.");
      localFormField.resetValues();
      localFormField.addValues(paramList);
      return;
    }
    throw new IllegalArgumentException("Couldn't find a field for the specified variable.");
  }

  public void setAnswer(String paramString, boolean paramBoolean)
  {
    String str;
    FormField localFormField = getField(paramString);
    if (localFormField == null)
      throw new IllegalArgumentException("Field not found for the specified variable name.");
    if (!("boolean".equals(localFormField.getType())))
      throw new IllegalArgumentException("This field is not of type boolean.");
    if (paramBoolean)
      str = "1";
    while (true)
    {
      setAnswer(localFormField, str);
      return;
      str = "0";
    }
  }

  public void setDefaultAnswer(String paramString)
  {
    if (!(isSubmitType()))
      throw new IllegalStateException("Cannot set an answer if the form is not of type \"submit\"");
    FormField localFormField = getField(paramString);
    if (localFormField != null)
    {
      localFormField.resetValues();
      Iterator localIterator = localFormField.getValues();
      while (true)
      {
        if (!(localIterator.hasNext()))
          return;
        localFormField.addValue((String)localIterator.next());
      }
    }
    throw new IllegalArgumentException("Couldn't find a field for the specified variable.");
  }

  public void setInstructions(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    StringTokenizer localStringTokenizer = new StringTokenizer(paramString, "\n");
    while (true)
    {
      if (!(localStringTokenizer.hasMoreTokens()))
      {
        this.dataForm.setInstructions(localArrayList);
        return;
      }
      localArrayList.add(localStringTokenizer.nextToken());
    }
  }

  public void setTitle(String paramString)
  {
    this.dataForm.setTitle(paramString);
  }
}